/*
 * GUser.java
 * Created on 18 juin 2007
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.data.dao.user;


import java.util.GregorianCalendar;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import com.hockeo.vhbase.data.dao.IGEntity;
import com.hockeo.vhbase.data.dao.club.GClub;

/**
 * Base class for human and non-human users that participate in the game.
 *
 * @version $Id$
 * @author jjanke
 */
@Entity
@Table( name = "m_user" )
@Inheritance( strategy = InheritanceType.SINGLE_TABLE )
@DiscriminatorColumn( name = "type", discriminatorType = DiscriminatorType.STRING )
public abstract class GUser implements IGEntity
{
  @Id
  @Column( name = "id" )
  private String            d_strId;

  @Column( name = "inscription_date" )
  private GregorianCalendar d_calInscriptionDate;

  @Column( name = "termination_date" )
  private GregorianCalendar d_calTerminationDate;

  @OneToMany( mappedBy = "d_user" )
  @OrderBy( "d_strId" )
  private List<GClub>       d_listCurrentClubs;

  /** Default parameterless constructor. */
  protected GUser()
  {}

  public GregorianCalendar getInscriptionDate()
  {
    return d_calInscriptionDate;
  }

  public GregorianCalendar getTerminationDate()
  {
    return d_calTerminationDate;
  }

  public List<GClub> getCurrentClubs()
  {
    return d_listCurrentClubs;
  }

  public String getId()
  {
    return d_strId;
  }

  /**
   * Returns the the user type code. This is usually a two letter upper case code which is
   * used in all resources related to users (like string resources, icons etc.).
   */
  public abstract String getTypeCode();

  public boolean instanceOf( Class<? extends IGEntity> classOther )
  {
    return classOther.isAssignableFrom( getGEntityClass() );
  }

  /*
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals( Object obj )
  {
    try
    {
      return d_strId.equals( ( (GUser) obj ).getId() );
    }
    catch ( Exception ex ) // especially ClassCastException(s) may happen
    {
      return false;
    }
  }

  /*
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode()
  {
    return d_strId.hashCode();
  }

  /*
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    return d_strId;
  }
}
